<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import type { FormState } from '@/types/auth'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 加载状态
const loading = ref(false)

// 表单数据
const formState = reactive<FormState>({
  username: '',
  password: '',
  remember: true
})

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleSubmit = async () => {
  try {
    loading.value = true
    await userStore.login(formState)
    message.success('登录成功')
    router.push('/dashboard')
  } catch (error: any) {
    message.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-title">
        <h1>高校文件收集系统</h1>
        <p>University Document Collection System</p>
      </div>
      <div class="login-box">
        <div class="login-form-title">
          <h2>用户登录</h2>
          <p>欢迎回来！请登录您的账号</p>
        </div>
        <a-form
          :model="formState"
          :rules="rules"
          class="login-form"
          @finish="handleSubmit"
        >
          <a-form-item name="username">
            <a-input
              v-model:value="formState.username"
              placeholder="请输入用户名"
              size="large"
            >
              <template #prefix>
                <UserOutlined />
              </template>
            </a-input>
          </a-form-item>
          <a-form-item name="password">
            <a-input-password
              v-model:value="formState.password"
              placeholder="请输入密码"
              size="large"
            >
              <template #prefix>
                <LockOutlined />
              </template>
            </a-input-password>
          </a-form-item>
          <a-form-item>
            <a-checkbox v-model:checked="formState.remember">记住密码</a-checkbox>
          </a-form-item>
          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              :loading="loading"
              class="login-button"
              block
              size="large"
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
html, body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('https://wkq-img.oss-cn-chengdu.aliyuncs.com/%E3%80%90%E5%93%B2%E9%A3%8E%E5%A3%81%E7%BA%B8%E3%80%91%E5%8A%A8%E7%94%BB%E8%A7%92%E8%89%B2-%E5%93%AA%E5%90%92.png') no-repeat center center;
  background-size: cover;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }

  .login-content {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 40px;
    z-index: 1;
    padding: 20px;

    .login-title {
      text-align: center;
      color: white;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
      
      h1 {
        font-size: 48px;
        font-weight: bold;
        margin-bottom: 16px;
        letter-spacing: 2px;
      }
      
      p {
        font-size: 24px;
        opacity: 0.9;
      }
    }

    .login-box {
      width: 400px;
      padding: 40px;
      background: rgba(255, 255, 255, 0.95);
      border-radius: 8px;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);

      .login-form-title {
        text-align: center;
        margin-bottom: 40px;
        
        h2 {
          font-size: 28px;
          color: #1890ff;
          margin-bottom: 8px;
        }
        
        p {
          font-size: 16px;
          color: #666;
        }
      }
      
      .login-form {
        :deep(.ant-form-item) {
          margin-bottom: 24px;
        }
        
        .login-button {
          height: 44px;
          font-size: 16px;
        }
        
        :deep(.ant-input-affix-wrapper) {
          height: 44px;
          padding: 0 16px;
          
          .anticon {
            color: #bfbfbf;
          }
        }
        
        :deep(.ant-checkbox-wrapper) {
          color: #666;
        }
      }
    }
  }
}

// 响应式处理
@media screen and (max-width: 1200px) {
  .login-container {
    .login-content {
      .login-title {
        h1 {
          font-size: 36px;
        }
        p {
          font-size: 20px;
        }
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .login-container {
    .login-content {
      padding: 20px;
      
      .login-title {
        display: none;
      }

      .login-box {
        width: 100%;
        max-width: 400px;
      }
    }
  }
}
</style> 